//
//  CXHImageViewScrollView2.m
//  11102(封装)
//
//  Created by student on 16/2/29.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "CXHImageViewScrollView2.h"

@interface CXHImageViewScrollView2()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end




@implementation CXHImageViewScrollView2
// 类方法，创建一个QXHImageScrollView对象
+ (instancetype)imageScrollView
{
    return [[NSBundle mainBundle]loadNibNamed:@"CXHImageViewScrollView2" owner:nil options:nil][0];
}

-(void)setImageNames:(NSArray *)imageNames
{
    _imageNames=imageNames;
    CGFloat width=self.scrollView.bounds.size.width;
    CGFloat height=self.scrollView.bounds.size.height;
    for (int i=0; i<self.imageNames.count; i++) {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*width, 0, width, height)];
        imageView.image=[UIImage imageNamed:imageNames[i]];
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize=CGSizeMake(self.imageNames.count*width, height);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setCurColor:(UIColor *)curColor
{
    _curColor=curColor;
    self.pageControl.currentPageIndicatorTintColor=curColor;
}
-(void)setOtherColor:(UIColor *)otherColor
{
    _otherColor=otherColor;
    self.pageControl.pageIndicatorTintColor=otherColor;
 
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    // 设定scorllView的位置和尺寸
    self.scrollView.frame=self.bounds;
    
    CGFloat width=self.scrollView.bounds.size.width;
    CGFloat height=self.scrollView.bounds.size.height;
    for (int i=0; i<self.imageNames.count; i++) {
        UIImageView *imageView=self.scrollView.subviews[i];
        imageView.frame=CGRectMake(width*i, 0, width, height);
    }
    self.scrollView.contentSize=CGSizeMake(width*self.imageNames.count, height);
    // 设定pageControl的位置和尺寸
    
    
    CGFloat pageW=100;
    CGFloat pageH=20;
    CGFloat pageX=self.bounds.size.width/2-pageW/2;
    CGFloat pageY=self.bounds.size.height-pageH;
    
    self.pageControl.frame=CGRectMake(pageX, pageY, pageW, pageH);
   
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage=scrollView.contentOffset.x/320;
}
@end
